# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: xguo

from coin.base.book import BookBuilderBase
from coin.exchange.liqui_v3.kr_rest.product import LiquiProduct


class BookEntry(object):
  def __init__(self, price, qty):
    self.price = float(price)
    self.qty = float(qty)


class BookBuilder(BookBuilderBase):
  ProductType = LiquiProduct

  def __init__(self, native_symbol):
    super().__init__("Liqui", native_symbol)

  def update(self, bids: list, asks: list):
    self._bid_price_map.clear()
    for data in bids:
      price = float(data[0])
      qty = float(data[1])
      self._insert_bid(price, qty)
    self._ask_price_map.clear()
    for data in asks:
      price = float(data[0])
      qty = float(data[1])
      self._insert_ask(price, qty)

  def _insert_bid(self, price, qty):
    assert price > 0, price
    assert qty > 0, qty
    entry = BookEntry(price, qty)
    self._bid_price_map[price] = entry

  def _insert_ask(self, price, qty):
    assert price > 0, price
    assert qty > 0, qty
    entry = BookEntry(price, qty)
    self._ask_price_map[price] = entry
